using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace BlockOS.Client
{
    [RequireComponent(typeof(CanvasRenderer))]
    public class LateralGradientGraphic : Graphic
    {
        [SerializeField]
        [Range(0.0f, 0.5f)]
        private float m_LeftGradientRate;
        [SerializeField]
        [Range(0.0f, 0.5f)]
        private float m_RightGradientRate;

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            var r = GetPixelAdjustedRect();
            var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height);
            var lmr = m_LeftGradientRate;
            var rmr = 1.0f - m_RightGradientRate;
            var lm = v.x + r.width * lmr;
            var rm = v.x + r.width * rmr;

            Color32 transparent = color;
            transparent.a = 0;
            Color32 color32 = color;
            vh.Clear();
            vh.AddVert(new Vector3(v.x, v.y), transparent, new Vector2(0f, 0f)); // lb
            vh.AddVert(new Vector3(v.x, v.w), transparent, new Vector2(0f, 1f)); // lt
            vh.AddVert(new Vector3(lm, v.w), color32, new Vector2(lmr, 1f)); // lmt
            vh.AddVert(new Vector3(lm, v.y), color32, new Vector2(lmr, 0f)); // lmb
            vh.AddVert(new Vector3(rm, v.y), color32, new Vector2(rmr, 0f)); // rmb
            vh.AddVert(new Vector3(rm, v.w), color32, new Vector2(rmr, 1f)); // rmt
            vh.AddVert(new Vector3(v.z, v.w), transparent, new Vector2(1f, 1f)); // rt
            vh.AddVert(new Vector3(v.z, v.y), transparent, new Vector2(1f, 0f)); // rb

            vh.AddTriangle(0, 1, 2);
            vh.AddTriangle(2, 3, 0);
            vh.AddTriangle(3, 2, 5);
            vh.AddTriangle(5, 4, 3);
            vh.AddTriangle(4, 5, 6);
            vh.AddTriangle(6, 7, 4);
        }
    }
}
